<?php
class uploadModel extends baseModel{
    protected $table = 'upload';

    public function get_upload_image($button,$id,$editor='',$pic=''){
        $html="<script>
        Do.ready('base','dialog', function() {
            $('#".$button."').click(function(){
                urldialog({
                    title:'图片上传',
                    url:'".url('editor/upload/image_upload',array('id'=>$id,'editor'=>$editor,'pic'=>$pic))."',
                    width:615,
                    height:150
                });
            });
        });
        </script>
        ";
        return $html;
    }

    public function get_upload_file($button,$id){
        $html="<script>
        Do.ready('base','dialog', function() {
            $('#".$button."').click(function(){
                urldialog({
                    title:'文件上传',
                    url:'".url('editor/upload/file_upload',array('id'=>$id))."',
                    width:615,
                    height:100
                });
            });
        });
        </script>
        ";
        return $html;
    }

    public function get_upload_images($button,$id){
        $html="<script>
        Do.ready('base','dialog', function() {
            $('#".$button."').click(function(){
                urldialog({
                    title:'多图片上传',
                    url:'".url('editor/upload/images_upload',array('id'=>$id))."',
                    width:615,
                    height:410
                });
            });
        });
        </script>
        ";
        return $html;
    }

    public function get_image_cutout($button,$id,$pic=''){
        $html="<script>
        Do.ready('base','dialog', function() {
            $('#".$button."').click(function(){
                var file=$('#".$id."').val();
                if(!file){
                    art.dialog.tips('请先上传图片', 3);
                    return;
                }
                var file_str=file.substr(0,1);
                if(file_str!=='/'){
                    art.dialog.tips('请上传本地图片', 3);
                    return;
                }
                urldialog({
                    title:'图片处理',
                    url:'".url('editor/upload/image_cotout',array('id'=>$id,'pic'=>$pic))."&file='+$('#".$id."').val(),
                    width:680,
                    height:530
                });
            });
        });
        </script>
        ";
        return $html;
    }

    //添加附件到数据库
    public function add($data)
    {
        return $this->insert($data);
    }

    //获取关联ID
    public function get_relation($table,$id)
    {
        $this->table='upload_relation';
        $list=$this->select('`table`="'.$table.'" AND id='.$id);
        if(!empty($list)){
            foreach ($list as $value) {
                $file_id.=$value['file_id'].',';
            }
        }
        return ','.$file_id;
    }

    //获取关联列表
    public function get_relation_list($file_id)
    {
        if(empty($file_id)){
            return;
        }
        $array=explode(',',$file_id);
        if(empty($array)){
            return;
        }
        foreach ($array as $value) {
            $value=intval($value);
            if($value){
                $data[]=$this->find('id='.$value);
            }
        }
        return $data;
        
    }

    //获取特定类型文件
    public function get_list_type($id,$ext)
    {
        $ext_list=explode(',', $ext);
        foreach ($ext_list as $value) {
            $ext_data.= "'".$value."',";
        }
        return $this->select('id in('.trim($id,',').') AND ext in ('.trim($ext_data,',').')');
    }

    //关联附件表
    public function relation($table,$file_id,$id)
    {
        if(empty($file_id)){
            return;
        }
        $array=explode(',',$file_id);
        if(empty($array)){
            return;
        }
        //重建附件关联
        $this->table='upload_relation';
        $this->delete('`table`="'.$table.'" AND id='.$id);
        foreach ($array as $value) {
            $value=intval($value);
            if(!empty($value)){
                $data=array();
                $data['table']=$table;
                $data['id']=$id;
                $data['file_id']=$value;
                $this->insert($data);
            }
        }
        return;
    }

    //获取附件信息
    public function info($id)
    {
        $this->table='upload';
        return $this->find('id='.$id);
    }

    //删除附件
    public function del($id)
    {
        $this->table='upload';
        $info=$this->info($id);
        if(empty($info)){
            return;
        }
        $this->delete('id='.$id);
        $this->del_relation_file($id);
        @unlink(ROOT_PATH.$info['file']);
    }

    //删除附件
    public function del_file($table,$id)
    {
        $this->table='upload_relation';
        $list=$this->select('`table`="'.$table.'" AND id='.$id);
        if(!empty($list)){
            foreach ($list as $value) {
                $this->del($value['file_id']);
            }
        }
        $this->del_relation($table,$id);
        
    }

    //删除关联表
    public function del_relation($table,$id)
    {
        $this->table='upload_relation';
        $this->delete('`table`="'.$table.'" AND id='.$id);
    }

    //按文件ID删除关联
    public function del_relation_file($id)
    {
        $this->table='upload_relation';
        $this->delete('file_id='.$id);
    }

    //获取所有文件统计
    public function upload_count()
    {
        return $this->count();
    }
	
}